import { defineStore } from 'pinia'
import {DictDataModel} from "/@/api/system/model/DictModel";
import {listCacheDictDataByDictTypeCode, listDictDataByDictTypeCode} from "/@/api/system/DictApi";
import {DictMap} from "/@/utils/dict/Dict";

export const useDictStore = defineStore({
    id: 'dict',
    state: () => {
        return {
            dict: new Map() as Map<string,DictDataModel[]>
        }
    },
    getters: {

    },
    actions: {
       async getDict(dictTypeCode: string): Promise<DictDataModel[]>{
            let dictDatas = this.dict.get(dictTypeCode);
            if(!dictDatas){
                dictDatas = DictMap.get(dictTypeCode)
                if(!dictDatas){
                    let res = await listCacheDictDataByDictTypeCode(dictTypeCode);
                    dictDatas = res
                    this.dict.set(dictTypeCode,res)
                }
            }
            return dictDatas;
        }
    }
})

